package com.twu.biblioteca;

public class CheckOutBook implements MenuItem  {

    private Library library;
    private String menuString;

    CheckOutBook(Library library) {
        this.library = library;
        menuString = "Check Out Book";
    }

    public String getMenuString() {
        return menuString;
    }

    public MenuActionResult performMenuAction(UserInterface userInterface) {

        MenuActionResult menuActionResult = new MenuActionResult();
        String bookTitle = userInterface.getBookTitle();
        String bookAuthor = userInterface.getBookAuthor();
        int yearOfPublication = userInterface.getBookYearOfPublication();

        LibraryItem bookToCheckOut = new Book(bookTitle,bookAuthor,yearOfPublication);

        if (library.checkOut(bookToCheckOut)) {
            menuActionResult.setMenuActionDetails(true, "Enjoy your book!!");
        } else {
            menuActionResult.setMenuActionDetails(false, "Sorry that book is not available!");
        }
        userInterface.displayMenuActionResult(menuActionResult);
        return menuActionResult;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        CheckOutBook checkOutBook = (CheckOutBook) o;

        if (library != null ? !library.equals(checkOutBook.library) : checkOutBook.library != null) return false;
        if (menuString != null ? !menuString.equals(checkOutBook.menuString) : checkOutBook.menuString != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = library != null ? library.hashCode() : 0;
        result = 31 * result + (menuString != null ? menuString.hashCode() : 0);
        return result;
    }

}
